<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>父-子关系文档映射 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/parent-child-mapping.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/parent-child-mapping.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="modeling-your-data.html">数据建模</a></span>
»
<span class="breadcrumb-link"><a href="parent-child.html">父-子关系文档</a></span>
»
<span class="breadcrumb-node">父-子关系文档映射</span>
</div>
<div class="navheader">
<span class="prev">
<a href="parent-child.html">« 父-子关系文档</a>
</span>
<span class="next">
<a href="indexing-parent-child.html">构建父-子文档索引 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="parent-child-mapping"></a>父-子关系文档映射<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/404_Parent_Child/40_Parent_child.asciidoc">edit</a>
</h2>
</div></div></div>
<p>建立父-子文档映射关系时只需要指定某一个文档类型(type)是另一个文档类型(type)的父亲。   
这个关系必须在创建索引时完成，或者在创建子类型之前使用<code class="literal">update-mapping</code>API完成。  
</p>
<p>
比如，假设我们有一家公司在许多城市都有分公司。 我们希望雇员与他们工作的分公司相关联。 我们需要能够搜索分公司、单个员工以及为特定分公司工作的员工，此时嵌套模型就无能无力了。 
当然，我们可以在这里使用<a class="xref" href="application-joins.html" title="应用层联接">应用层联接(application-side-joins)</a>或<a class="xref" href="denormalization.html" title="去规范化数据">去规范化数据(data denormalization)</a>，但出于演示目的，我们将使用父-子关系。 
</p>

<p>我们需要告诉Elasticsearch，在创建员工<code class="word">employee</code>文档类型<code class="literal">type</code>时，指定分公司<code class="literal">branch</code>的文档类型<code class="literal">type</code>为其父亲。
</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">PUT /company
{
  "mappings": {
    "branch": {},
    "employee": {
      "_parent": {
        "type": "branch" <a id="CO278-1"></a><i class="conum" data-value="1"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO278-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="word">employee</code>文档是<code class="word">branch</code>文档的子文档。</p>
</td>
</tr>
</table>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="parent-child.html">« 父-子关系文档</a>
</span>
<span class="next">
<a href="indexing-parent-child.html">构建父-子文档索引 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>